let timer
let ts = 0
let timeTotal = 0

loading()
renderNavBar()

// 左侧菜单
function renderNavBar(){
    const navs = [
        {uri:'faceDetection',name:'人脸检测'},
        {uri:'faceLandmarkDetection',name:'人脸标识点检测'},
        {uri:'faceExpressionRecognition',name:'人脸表情识别'},
        {uri:'ageAndGenderRecognition',name:'年龄和性别识别'},
        {uri:'faceRecognition',name:'人脸识别'},
        {uri:'faceExtraction',name:'人脸提取'},
        {uri:'videoFaceTracking',name:'视频人脸跟踪'},
        {uri:'webcamFaceDetection',name:'网络摄像机人脸检测'},
        {uri:'webcamFaceLandmarkDetection',name:'网络摄像头人脸地标检测'},
        {uri:'webcamFaceExpressionRecognition',name:'网络摄像头人脸表情识别'},
        {uri:'webcamAgeAndGenderRecognition',name:'网络摄像机年龄与性别识别'},
        {uri:'bbtFaceLandmarkDetection',name:'BBT人脸标志点检测'},
        {uri:'bbtFaceSimilarity',name:'BBT人脸相似性'},
        {uri:'bbtFaceMatching',name:'BBT人脸匹配'},
        {uri:'bbtFaceRecognition',name:'BBT人脸识别'},
        {uri:'batchFaceLandmarks',name:'批量人脸标志检测'},
        {uri:'batchFaceRecognition',name:'批量人脸识别'},
    ]

    const navBar = document.createElement('div')
    navBar.id = 'navBar'
    const body = document.querySelector('body')
    body.prepend(navBar)

    navs.forEach(nav=>{
        const navDom = document.createElement('div')
        navDom.className = 'nav'
        navDom.innerHTML = `<a href="/${nav.uri}/index.html" id="${nav.uri}">${nav.name}</a>`
        navBar.appendChild(navDom)
    })

    const uriName = location.pathname.split('/')[1]
    const activeNavEl = document.querySelector('#'+uriName)
    activeNavEl.className = 'active'

}

// loading动画
function loading(){
    ts = Date.now()
    const dom = document.createElement('div')
    dom.className = 'loading'
    dom.innerHTML = '<div id="loading-image"><img src="http://127.0.0.1:8888/images/loading.gif" /></div>'
    document.querySelector('body').prepend(dom)

    const domImg = document.getElementById('loading-image')
    const timeDom = document.createElement('span')
    timeDom.id = 'timeTotal'
    domImg.appendChild(timeDom)

    const timeDom2 = document.createElement('div')
    timeDom2.id = 'timeTotal2'
    document.querySelector('.page-container').prepend(timeDom2)
    
    getTimer()
}

// 耗费时间
function getTimer(){
    clearTimeout(timer)
    timer = setTimeout(()=>{
        timeTotal = Date.now()-ts 
        const timeTotalDom = document.querySelector('#timeTotal')
        if(timeTotalDom){
            document.querySelector('#timeTotal2').innerHTML = '共耗时：'+(timeTotal/1000)+'s'
            timeTotalDom.innerHTML = (timeTotal/1000)+'s'
            getTimer()
        }
    },300)
}

// 关闭loading
window.closeLoading = ()=>{
    const dom = document.querySelector('.loading')
    if(dom){
        dom.remove()
    }
}